package com.telit.firecommand.controller;


import com.telit.common.annotation.Log;
import com.telit.common.constant.Constant;
import com.telit.common.entity.DataReturn;
import com.telit.common.entity.PageData;
import com.telit.common.enums.BusinessOperatorType;
import com.telit.common.enums.BusinessType;
import com.telit.common.enums.OperatorType;
import com.telit.common.enums.ServiceName;
import com.telit.firecommand.dto.DistrictCountDTO;
import com.telit.firecommand.dto.ForesterDTO;
import com.telit.firecommand.dto.TaskReleaseDTO;
import com.telit.firecommand.service.impl.ForesterService;
import com.telit.firecommand.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @Auther amy
 * @Describe 护林员数据展示控制层
 */
@RestController
@Api(tags = "护林员接口相关", description = "forester")
@RequestMapping("/forester")
public class ForesterController {

    @Autowired
    private ForesterService foresterService;

    @GetMapping(value = "/info", produces = "application/json;charset=UTF-8")
    @ApiOperation("查找所有护林员信息")
    public DataReturn<List<ForesterDTO>> listOnlineInfo(
            @RequestParam(value = "status", required = false) Integer status,
            @RequestParam(value = "orgId", required = false) Long orgId,
            @RequestParam(value = "longitude", required = false) String longitude,
            @RequestParam(value = "latitude", required = false) String latitude,
            @RequestParam(value = "areaCode", required = false) String areaCode,
            @RequestParam(value = "radius", required = false) String radius) {
        List<ForesterDTO> foresterDTOS = foresterService.foresterSelect(status, null, orgId, longitude, latitude,
                areaCode, radius);
        return DataReturn.ok(foresterDTOS);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "name", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "status", value = "status", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "districtCode", value = "行政区划", required = true, paramType = "query", dataType = "String"),
    })
    @GetMapping(value = "/tree-district", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "根据父行政区划编码查询子类集合，初始化时请传递省份code（安徽省：34）", httpMethod = Constant.REQUEST_METHOD_GET)
    public DataReturn<DistrictCountDTO> listByDistrict(@RequestParam(required = false) String name,
                                                       @RequestParam(required = false) Integer status,
                                                       @RequestParam(value = "districtCode") String districtCode) {
        return DataReturn.ok(foresterService.listByDistrict(status, name, districtCode));
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "用户真实姓名", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "onlineStatus", value = "在线状态 0-离线 1-在线", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "regionCode", value = "个人所属行政区划编码", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "current", value = "当前页数", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "size", value = "每页条数", required = true, paramType = "query", dataType = "int"),
    })
    @GetMapping(value = "/qry-list", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "分页查询护林员列表", httpMethod = Constant.REQUEST_METHOD_GET)
    public DataReturn<PageData<List<ForesterVO>>> qryList(@RequestParam(required = false) String name,
                                                          @RequestParam(required = false) Integer onlineStatus,
                                                          @RequestParam(value = "regionCode", required = false) String regionCode,
                                                          @RequestParam(value = "current", defaultValue = "1") Integer current,
                                                          @RequestParam(value = "size", defaultValue = "20") Integer size) {
        return DataReturn.ok(foresterService.qryList(onlineStatus, name, regionCode, current, size));
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "longitude", value = "经度", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "latitude", value = "纬度", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "regionCode", value = "个人所属行政区划编码", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "distance", value = "距离（千米），默认5千米", paramType = "query", dataType = "String"),
    })
    @GetMapping(value = "/qry-list-by-distance", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "查询几公里范围内的护林员列表", httpMethod = Constant.REQUEST_METHOD_GET)
    public DataReturn<List<ForesterVO>> qryListByDistance(@RequestParam String longitude,
                                                          @RequestParam String latitude,
                                                          @RequestParam String regionCode,
                                                          @RequestParam(required = false, value = "distance", defaultValue = "5") String distance) {
        return DataReturn.ok(foresterService.qryListByDistance(longitude, latitude, regionCode, distance));
    }

    //根据护林员id查询护林员详细信息
    @GetMapping(value = "/foresterInfoById", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "根据护林员id查询护林员详细信息", httpMethod = Constant.REQUEST_METHOD_GET)
    public DataReturn<ForesterVO> foresterInfoById(@RequestParam(required = true) Long id) {
        return DataReturn.ok(foresterService.foresterInfoById(id));
    }

    //根据批量查询护林员信息接口
    @GetMapping(value = "/batchFindForesterInfo", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "批量查询护林员信息", httpMethod = Constant.REQUEST_METHOD_GET)
    public DataReturn<List<ForesterVO>> batchFindForesterInfo(@RequestParam(required = true) List<Long> ids) {
        return DataReturn.ok(foresterService.batchFindForesterInfo(ids));
    }


//    @GetMapping(value = "/districtForestCount/{code}", produces = "application/json;charset=UTF-8")
//    @ApiOperation("依据上级行政区划编码查询下级行政区划列表以及护林员数量")
//    public DataReturn districtForestCount(@PathVariable(value = "code") String code) {
//        return DataReturn.ok(foresterService.districtForestCount(code));
//    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "name", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "districtCode", value = "行政区划", required = true, paramType = "query", dataType = "String"),
    })
    @GetMapping(value = "/count", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "查找护林员在线离线个数", httpMethod = Constant.REQUEST_METHOD_GET)
    public DataReturn countList(@RequestParam(required = false) String name,
                                @RequestParam(value = "districtCode") String districtCode) {
        Map<String, Integer> resultMap = new HashMap<>();
        resultMap.put("onlineCount", foresterService.countByCondition(districtCode, 1, name));
        resultMap.put("offlineCount", foresterService.countByCondition(districtCode, 0, name));
        resultMap.put("count", foresterService.countByCondition(districtCode, null, name));
        return DataReturn.ok(resultMap);
    }


    //护林员上报相关接口
    @Log(service = ServiceName.FIRE_ALARMS, businessType = BusinessType.BUSINESS_LOG, operatorType = OperatorType.WEB, businessOperatorType = BusinessOperatorType.RWXF)
    @PostMapping(value = "/releaseTask")
    @ApiOperation("护林员上报相关接口")
    public DataReturn<Boolean> releaseTask(@RequestBody TaskReleaseDTO taskReleaseDTO) {
        foresterService.releaseTask(taskReleaseDTO);
        return DataReturn.ok();
    }


    @GetMapping(value = "/countForTask")
    @ApiOperation("查询tab页面统计数据")
    public DataReturn<List<FeignTaskNumVo>> countForTask(
            @RequestParam(value = "district", required = false) String district,
            @RequestParam(value = "userId", required = false) Long userId) {
        List<FeignTaskNumVo> taskNumVoList = foresterService.countForTask(district, userId);
        return DataReturn.ok(taskNumVoList);
    }

    //查询巡护任务接口查询
    @GetMapping(value = "/patrolList")
    @ApiOperation("分页查询巡护记录列表")
    public DataReturn<PageData<List<PatPersonPatrolInfoVo>>> patrolList(
            @RequestParam(value = "startTime", required = false) String startTime,
            @RequestParam(value = "endTime", required = false) String endTime,
            @RequestParam(value = "regionCode", required = false) String regionCode,
            @RequestParam(value = "current", defaultValue = "1") Integer current,
            @RequestParam(value = "size", defaultValue = "20") Integer size,
            @RequestParam(value = "protectorId", required = false) Long protectorId,
            @RequestParam(value = "personType", required = false) String personType) {
        PageData pageData = foresterService.patrolList(startTime, endTime, regionCode, current, size, protectorId,
                personType);
        return DataReturn.ok(pageData);
    }

    @GetMapping(value = "/getPatrolInfoDetail")
    @ApiOperation("依据巡护、巡林记录ID,获取详情")
    public DataReturn<TaskInfoVo> getPatrolInfoDetail(@RequestParam(value = "id") Long id) {
        TaskInfoVo taskInfoVo = foresterService.getPatrolInfoDetail(id);
        return DataReturn.ok(taskInfoVo);
    }


    @GetMapping(value = "/getTaskListByPerson")
    @ApiOperation("分页查询巡护任务列表  taskTypeGrade任务类型 1 日常  2指定 3 热点 4 火情核实 5 火情监测 6 火烧地")
    public DataReturn<PageData<List<TaskInfoListVo>>> getTaskListByPerson(
            @RequestParam(value = "releaseStartTime", required = false) String releaseStartTime,
            @RequestParam(value = "releaseEndTime", required = false) String releaseEndTime,
            @RequestParam(value = "taskStartTime", required = false) String taskStartTime,
            @RequestParam(value = "taskEndTime", required = false) String taskEndTime,
            @RequestParam(value = "state", required = false) Integer state,
            @RequestParam(value = "taskTypeGrade", required = false) Integer taskTypeGrade,
            @RequestParam(value = "regionCode", required = false) String regionCode,
            @RequestParam(value = "current", defaultValue = "1") Integer current,
            @RequestParam(value = "size", defaultValue = "20") Integer size,
            @RequestParam(value = "protectorId", required = false) Long protectorId) {
        PageData pageData = foresterService.getTaskListByPerson(releaseStartTime, releaseEndTime, taskStartTime,
                taskEndTime, state, taskTypeGrade, regionCode, current, size, protectorId);
        return DataReturn.ok(pageData);
    }


    @GetMapping(value = "/getTaskDetail")
    @ApiOperation("根据任务ID获取任务详情（包含任务信息、反馈信息、巡护记录等")
    public DataReturn<TaskInfoVo> getTaskDetail(@RequestParam(value = "taskId") Long taskId,
                                                @RequestParam(value = "userId") Long userId) {
        TaskInfoVo taskInfoVo = foresterService.getTaskDetail(taskId, userId);
        return DataReturn.ok(taskInfoVo);
    }


    @ApiOperation(value = "根据火情id查询对应护林员")
    @GetMapping(path = "/findForesterByFireId")
    public DataReturn<PageData<List<FireHlyListVo>>> findForesterByFireId(
            @RequestParam("fireId") String fireId) {
        PageData pageData = foresterService.findForesterByFireId(fireId);
        return DataReturn.ok(pageData);
    }

    @ApiOperation(value = "根据火情id + 护林员id，获取执行任务列表")
    @GetMapping(path = "/findTaskByForesterIdAndFireId")
    public DataReturn<PageData<List<FireHlyTaskListVo>>> findTaskByForesterIdAndFireId(
            @RequestParam("fireId") String fireId,
            @RequestParam("personId") String personId) {

        PageData pageData = foresterService.findTaskByForesterIdAndFireId(fireId, personId);
        return DataReturn.ok(pageData);
    }

    /**
     * 查询临时账号，为了满足演示
     */
    @GetMapping(path = "/qry-temp-forester")
    public DataReturn<List<ForesterDTO>> qryTempForester(
            @RequestParam(value = "longitude", required = false) String longitude,
            @RequestParam(value = "latitude", required = false) String latitude) {
        return DataReturn.ok(foresterService.qryTempForester(longitude, latitude));
    }

}
